<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection"
          content="telephone=no,email=no,date=no,address=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="msapplication-tap-highlight" content="no">
    <title>支付订单信息</title>
    <link rel="stylesheet" type="text/css" href="../css/phone.css">
    <link rel="stylesheet" type="text/css" href="../css/demos.css">
    <link rel="stylesheet" type="text/css" href="../statics/aui/css/aui.css"/>
    <link rel="stylesheet" type="text/css" href="../css/weui.min.css"/>
    <script src="../js/jquery.min.js"></script>
    <script src="../js/jquery-weui.min.js"></script>


</head>
<body>

<div class="page">
    <div class="page__hd">
        <h1 class="page__title" align="center">支付订单信息</h1>
    </div>
    <div class="page__bd">
        <div class="weui-form-preview">
            <div class="weui-form-preview__bd">
                <div class="weui-form-preview__item" style="line-height: 40px;">
                    <label class="weui-form-preview__label">商品名称</label>
                    <input type="hidden" name="payremark" id="payremark">
                    <em class="weui-form-preview__value" id="payremarkVal"></em>
                </div>
                <div class="weui-form-preview__item" style="border-top: 1px solid #D5D5D6;line-height: 40px;">
                    <label class="weui-form-preview__label">订单编号</label>
                    <em class="weui-form-preview__value" id="recordNoVal"></em>
                </div>
                <div class="weui-form-preview__item" style="border-top: 1px solid #D5D5D6;line-height: 40px;">
                    <label class="weui-form-preview__label">商家名称</label>
                    <em class="weui-form-preview__value" id="merchantName"></em>
                </div>
                <div class="weui-form-preview__item" style="border-top: 1px solid #D5D5D6;line-height: 40px;">
                    <label class="weui-form-preview__label">支付金额</label>
                    <input type="hidden" name="amount" id="amount">
                    <em class="weui-form-preview__value">¥<label id="amountVal"></label>元</em>
                </div>
            </div>
        </div>
        <div class="page__hd">
            <label style="color: #999999;line-height: 45px;margin-left:15px ">选择支付方式</label>
        </div>
        <div class="weui-form-preview">
            <div class="weui-form-preview__bd">
                <div class="weui-form-preview__item" style="line-height: 40px;">
                    <div class="aui-btn aui-btn-info  aui-btn-block" onclick="javascript:;">微信支付</div>
                </div>
            </div>
            <div class="weui-form-preview__bd">
                <div class="weui-form-preview__item" style="line-height: 40px;">
                    <div class="aui-btn aui-btn-info  aui-btn-block" onclick="javascript:;">支付宝支付</div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="dialogs">
    <div class="js_dialog" id="iosDialog2" style="display: none;">
        <div class="weui-mask"></div>
        <div class="weui-dialog">
            <div class="weui-dialog__bd" id="DialogMessage"></div>
            <div class="weui-dialog__ft">
                <a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary DeleteDefault">确定</a>
            </div>
        </div>
    </div>
</div>


<script type="text/javascript">
    var merchantId;//商户
    var codeNo;//收款码编号
    var payMoney;///付款金额
    var userAgent;
    var resource = ''; //支付来源
    var shopName;//商户名称
    var logo;//商户logo
    var baseUrl;//域名地址
    var baseUrlWechat;//域名地址
    var sponsorOrderNo,payremark,notifyUrl,sign,payId;
    $(function () {
        //获取网页参数
        (function ($) {
            $.getUrlParam = function (name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null)
                    return unescape(r[2]);
                return null;
            }
        })(jQuery);
        var ua = navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == "micromessenger") {
            resource = '0';
        }else if(ua.match(/AlipayClient/i) == "alipayclient"){ //
            resource = '1';
        } else {
            resource = '-1';
        }

        sponsorOrderNo = $.getUrlParam('sponsorOrderNo');//发起方订单号
        payMoney = $.getUrlParam('payMoney');////付款金额
        payremark = decodeURI($.getUrlParam('payremark'));//订单说明
        notifyUrl = $.getUrlParam('notifyUrl');//回调地址
        sign = $.getUrlParam('sign');//签名+key
        payId = $.getUrlParam('payId');//参数key

        codeNo = $.getUrlParam('codeNo');
        merchantId = $.getUrlParam('merchantId');
        userAgent = $.getUrlParam("User-Agent");
        shopName = decodeURI($.getUrlParam("shopName"));
        logo = decodeURI($.getUrlParam("logo"));
        baseUrl = decodeURI($.getUrlParam("baseUrl"));
        baseUrlWechat = decodeURI($.getUrlParam("baseUrlWechat"));
        ///https://www.***.cn/pay-manager/merchantinfo/lookImage?filePath=/D2017092916000000002/merchantInfo/234.jpg&imgSize=300x200
            //$('#logoUrl').attr('src',"uploadQRCode/hnhwly.jpg");//uploadQRCode/hnhwly.jpg
            $('#merchantName').text("shopName");
            //$('#merchantName').text(shopName);
        $("#amount").val(payMoney);
        $("#amountVal").text(payMoney);
        $("#payremark").val(payremark);
        $("#payremarkVal").text(payremark);
        $("#recordNoVal").text(sponsorOrderNo);
    });

    //支付提交
    function submitBtn() {
        $("#buttonQrzf").attr({ disabled: "disabled" });
        var data = {
            resource: resource,//来源支付宝还是微信
            busId: merchantId,//商户
            codeNo: codeNo,//收款码编号
            payType:'1',//区分第三方还是 还是二维码发起支付

            sponsorOrderNo: sponsorOrderNo,//发起方订单号
            payMoney: payMoney,////付款金额
            payremark: payremark,///订单说明
            notifyUrl: notifyUrl,/////回调地址
            sign: sign,////签名+key
            payId: payId,////签名+key

            category: '0',
            status: "0",
            amount: $("#amount").val(),
            payremark: $("#payremark").val(),
        };
        $.ajax({
            type: "POST",
            url: baseUrl+"payorder/save",
            contentType: 'application/json;charset=utf-8',
            data: JSON.stringify(data),
            dataType: 'json',
            success: function (result) {
                if (result.code == 0) {
                    if (userAgent == 'MicroMessenger') {
                        //wxJsPay(result.token_id);
                        //callpay(result);
                        callpay(result.payInfo);
                    } else if (userAgent == 'AlipayClient') {
                        aliPay(result);
                    } else {
                        $("#DialogMessage").html(result.msg);
                        $iosDialog2.fadeIn(200);
                        //window.location.href = "http://www.***.cn/wechat/pay/wxpay/pay_failed.html";
                    }
                } else {
                    $("#DialogMessage").html(result.msg);
                    $iosDialog2.fadeIn(200);
                    //window.location.href = "http://www.***.cn/wechat/pay/wxpay/pay_failed.html";
                }
            }
        });
    }

    function jsApiCall(params) {
        WeixinJSBridge.invoke('getBrandWCPayRequest', params, function (res) {
            if (res.err_msg == "get_brand_wcpay_request:ok") {
                // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg 将在用户支付成功后返回ok，但并不保证它绝对可靠。
                window.location.href = baseUrlWechat+"pay_success.html"
            } else if (res.err_msg == "get_brand_wcpay_request:fail") {
                window.location.href = baseUrlWechat+"pay_failed.html"
            }
            WeixinJSBridge.call('closeWindow');
        });
    }

    function callpay(params) {
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        } else {
            jsApiCall(params);
        }
    }

    function aliPay(params) {
        /*  if (params.payUrl)
             window.location.href = params.payUrl; */
        AlipayJSBridge.call("tradePay", {
            tradeNO: params.tradeNO
        }, function (result) {
            if (result.resultCode == '9000') {
                window.location.href = baseUrlWechat+"pay_success.html"
            } else {
                window.location.href = baseUrlWechat+"pay_failed.html"
            }
            AlipayJSBridge.call('closeWebview');  //支付宝
        });
    }

    var oldVal = "";
    function validateAmount(obj) {
        var objVal = $(obj).val();
        var isNum = /^\d{1,6}([.]\d{0,2})?$/;
        if (objVal != "" && !isNum.test(objVal)) {
            $(obj).val(oldVal);
        } else {
            oldVal = objVal;
        }
    }

    var $iosDialog2 = $('#iosDialog2')
    $('#dialogs').on('click', '.DeleteDefault', function(){ //取消按钮事件
        $(this).parents('.js_dialog').fadeOut(200);
        $("#buttonQrzf").removeAttr("disabled");
    });

    function wxJsPay(params) {
        window.location.href = "https://pay.swiftpass.cn/pay/jspay?token_id=" + params;
    }

</script>
</body>
</html>